import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, Observer } from "rxjs";
import { DictionaryManageService } from '../../../system-manage/dictionary-manage/dictionary-manage.service';

@Injectable({
  providedIn: 'root'
})
export class SubwarehouseManageService {

  private kfListUrl = '/api/warehouse';
  private kqListUrl = '/api/warehouseArea'
  private kfListAllUrl = '/api/warehouse/tree'

  // 获取库房列表
  async getKFList(data): Promise<Node> {
    let dataJson = {
      code: data.code,
      name: data.name,
      materialCategory: data.materialCategory,
      page: data.page,
      size: data.size
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.get(this.kfListUrl, { params: dataJson }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  //新增修改库房
  async addEditKFList(data): Promise<Node> {
    let resources = []
    resources.push({
      id: data.id,
      code: data.code,
      address: data.address,
      category: data.category,
      independent: data.independent,
      materialCategory: data.materialCategory,
      name: data.name,
      warehouseAreas: data.warehouseAreas
    })
    return new Promise<Node>((resolve, reject) => {
      this.http.post(this.kfListUrl, resources, {}).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 删除库房
  async deletekf(data): Promise<Node> {
    let options = {
      headers: {
        "Content-Type": "application/json"
      },
      body: data
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.delete(this.kfListUrl, options).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 新增库区
  async addKQList(data): Promise<Node> {
    let resources = {
      area: data.area,
      bunker: data.bunker,
      category: data.category,
      code: data.code,
      description: data.description,
      ink: data.ink,
      name: data.name,
      warehouseId: data.warehouseId
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.post(this.kqListUrl, resources, {}).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 修改库区
  async editKQList(data): Promise<Node> {
    let resources = {
      id: data.id,
      area: data.area,
      bunker: data.bunker,
      category: data.category,
      code: data.code,
      description: data.description,
      ink: data.ink,
      name: data.name
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.put(this.kqListUrl, resources, {}).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 删除库区
  async deletekq(data): Promise<Node> {
    let options = {
      headers: {
        "Content-Type": "application/json"
      },
      body: data
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.delete(this.kqListUrl, options).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 存料\库房类型下拉菜单
  getclTypeList(data): Observable<any> {
    let dataJson = {
      ...data
    }
    return this.DictionaryManageService.getDetailAll(dataJson)
  }

  // 获取所有的库房 (移库入库申请)
  getKFAllList(): Observable<any> {
    return this.http.get(this.kfListAllUrl, {}).pipe()
  }
  // 查询库区
  getKQAllList(data): Observable<any> {
    let _dataJson = {
      ...data
    }
    return this.http.get(this.kqListUrl, {params:_dataJson}).pipe()
  }
  constructor(
    private http: HttpClient,
    private DictionaryManageService:DictionaryManageService
  ) { }
}
